#include "checkTwoPi.h"
void Check_Wave::check_glbarS() {
  Serial_SL ser_t;
  int serial_flag = 1;  // 1:save 2:load
  int serial_flag2 = 1; //
  cout << "serial choice 1: save  2: load ";
  cin >> serial_flag;
  cout << endl;

  std::ofstream file;
  Time_Count TC3;
  TC3.start();

  int k_mesh = 16;
  int p_mesh = 16;
  int n_max = 1;
  int l_max = 2;
  int glbarS = 4;

  Wave_Fun wave;
  if (serial_flag == 1) {
    wave.init(k_mesh, p_mesh, n_max, l_max, glbarS,0); // ( n_max, l_max, lbarS)
    // wave.glbarS_init(5, 5, 5); // lbar lbar12 lbar3 klamb_max
    wave.print("data/wavefun.dat");
    wave.print_Pmesh("data/pmesh.dat");
    // wave.glbarS_vec_cal();
    // wave.PFKglbarS_init();
    ser_t.save(wave, "serial_dat/Wave_serial.dat");
  } else {
    // ser_t.load(wave, "serial_dat/Wave_serial.dat");
    ser_t.load(wave, "serial_dat/Wave_serial.dat");
  }

  int size_klamb = 0;
  for (int l = 0; l < wave.klamb_state.size(); l++) {
    for (int i = 0; i < wave.klamb_state[l].size(); i++) {
      /*
      cout<<"l = "<<l<<"\t A = "<<wave.klamb_state[l][i].lamb12_p;
      cout<<"\t B = "<<wave.klamb_state[l][i].lamb12;
      cout<<"\t C = "<<wave.klamb_state[l][i].lamb3_p;
      cout<<"\t D = "<<wave.klamb_state[l][i].lamb3;
      cout<<endl;
      */
      size_klamb++;
    }
  }
  cout << "klamb_state = " << size_klamb << endl;

  int size_kpk = wave.kpk_state.size();
  int size_lbar = wave.lbar_state.size();
  int size_u = wave.legendreP_x.size();
  vector<vector<vector<double>>> G;
  G.resize(size_lbar);

  int S_index = 3;
  int s1 = 0;
  int s2 = 0;
  // int l=0;
  file.open("data/glbarS.bigdat");
  for (int l = 0; l < size_lbar; l++) {
    int lbar = wave.lbar_state[l].lbar;
    int lbar12 = wave.lbar_state[l].lbar12;
    int lbar3 = wave.lbar_state[l].lbar3;
    // cout<<l<<"\t :"<<lbar<<"\t"<<lbar12<<"\t"<<lbar3<<endl;
    double C = (2 * lbar + 1) * (2 * lbar3 + 1) * (2 * lbar12 + 1) / 8.0;
    G[l].resize(size_kpk);
    for (int i = 0; i < size_kpk; i++) {
      G[l][i].resize(size_kpk);
      // cout << i << endl;
      for (int j = 0; j < size_kpk; j++) {
        // G[i].push_back(wave.glbarS_cal(0, 0, 0, 0, 0, size_u, i, j));
        G[l][i][j] = wave.glbarS_vec[S_index][l][i][j];
        // G[l][i][j] = wave.glbarS_cal(s1,s2, lbar, lbar12, lbar3, p_mesh, i,
        // j);  G[i][j]=wave.glbarS_cal(0, 0, 1, 3, 4, p_mesh, i, j) ;
        file << l << "\t" << i << "\t" << j << "\t" << G[l][i][j] << endl;
        // double t=wave.glbarS_cal(0, 0, 0, 0, 0, size_u, i, j);
      }
    }
  }
  file.close();

  cout << "Time cost : " << TC3.sec() << "s" << endl;
  // cout<<wave.PFKglbarS_vec_cal2(9, 9, 2, 3, 1, 5, S_index, 0)<<endl;
  // cout << wave.glbarS_cal(0, 0, 0, 5, 5, p_mesh, 99, 99) << endl;

  file.open("data/ksq.dat");
  for (int i = 0; i < size_kpk; i++) {
    for (int j = 0; j < wave.p_mesh; j++) {
      file << i << "\t" << j << "\t" << wave.ksq[i][j] << endl;
    }
  }
  file.close();
  file.open("data/legendre_PlW.dat");
  for (int i = 0; i < wave.legendre_PlW[0].size(); i++) {
    file << i << "\t" << wave.legendre_PlW[0][i] << endl;
  }
  file.close();
  file.open("data/lbar_state.dat");
  for (int i = 0; i < wave.lbar_state.size(); i++) {
    file << wave.lbar_state[i].lbar << "\t" << wave.lbar_state[i].lbar12 << "\t"
         << wave.lbar_state[i].lbar3 << "\t" << endl;
  }
  file.close();

  file.open("data/nl_state.dat");
  for (int i = 0; i < wave.nl_state.size(); i++) {
    file << i << "\t" << wave.nl_state[i].n12 << "\t" << wave.nl_state[i].n3
         << "\t";
    file << wave.nl_state[i].l12 << "\t" << wave.nl_state[i].l3 << endl;
  }
  file.close();
}